import LocalStorageKey from '@/constant/LocalStorageKey'
import IUserSlice from '@/model/slice/IUserSlice'
import IMenuVO from '@/model/vo/menu/IMenuVO'
import IUserBaseInfoVO from '@/model/vo/user/IUserBaseInfoVO'
import { createSlice, PayloadAction } from '@reduxjs/toolkit'

const initialState: IUserSlice = {
  userBaseInfo: JSON.parse(
    localStorage.getItem(LocalStorageKey.USER_BASE_INFO) || '{}'
  ) as IUserBaseInfoVO,
  userMenuList: [] as IMenuVO[],
  currentRouter: {} as IMenuVO,
}

export const userSlice = createSlice({
  name: 'userSlice',
  initialState,
  reducers: {
    setUserBaseInfo: (state, action: PayloadAction<IUserBaseInfoVO>) => {
      state.userBaseInfo = action.payload
      localStorage.setItem(
        LocalStorageKey.USER_BASE_INFO,
        JSON.stringify(action.payload)
      )
    },
    setUserMenuList: (state, action: PayloadAction<IMenuVO[]>) => {
      state.userMenuList = action.payload
    },
    setCurrentRouter: (state, action: PayloadAction<IMenuVO>) => {
      state.currentRouter = action.payload
    },
  },
})

export const { setUserBaseInfo, setUserMenuList, setCurrentRouter } =
  userSlice.actions

export default userSlice.reducer
